package cq.com.cn.filter;


import cq.com.cn.utils.AjaxUtils;
import cq.com.cn.utils.Constants;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(urlPatterns={"*.do"})
public class CommonsFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setCharacterEncoding("UTF-8");
        HttpServletRequest req= (HttpServletRequest) servletRequest;
        HttpServletResponse response= (HttpServletResponse) servletResponse;
        String uri = req.getRequestURI();
        if(uri.contains("goodscard")){
            Object obj = req.getSession().getAttribute(Constants.SEESIONKEY);
            if(obj==null){
                AjaxUtils.showInfo(response,"请先登录!","/view/login.html");
                return;
            }
        }
        filterChain.doFilter(servletRequest,servletResponse);


    }
    @Override
    public void destroy() {

    }
}
